package de.fhd.pms.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.fhd.pms.model.Auto;
import de.fhd.pms.model.Fahrt;
import de.fhd.pms.model.Kunde;
import de.fhd.pms.model.Station;

/**
 * The Data access class for {@link Station} objects. DAO encapsulates all
 * accesses to the data source, including construction and dismantling of the
 * connection. All Interaction with the database regarding the entity bean
 * {@link Station} should be handled by this class! Its tasks:
 * <ul>
 * <li>set new objects of the class</li>
 * <li>modify or delete an object of the class</li>
 * <li>deliver all objects of the class</li>
 * <li>further functions in context to the class objects</li>
 * </ul>
 * 
 * @author Fischer, Neetzel & Six
 */
public class StationDao extends HibernateDaoSupport {
	private AutoDao autoDao;

	public void setAutoDao(AutoDao autoDao) {
		this.autoDao = autoDao;
	}
	
	

	/**
	 * Returns a single {@link Station} object by its primary DB key
	 * 
	 * @param id
	 *            the primary key of {@link Station}
	 * @return a single {@link Station}
	 */
	public Station findById(int id) {
		HibernateTemplate template = getHibernateTemplate();
		return (Station) template.get(Station.class, id);
	}

	/**
	 * Saves {@link Station} object specified by the parameter in the DB
	 * 
	 * @param station
	 *            is the object that'll be saved in the DB
	 * @return the object specified by the parameter
	 */
	public Station save(Station station) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(station);
		return station;
	}

	public String getGoogle() {
		String googleLink = "http://maps.google.com/maps/api/staticmap?center=48.139058,11.580276&zoom=11&size=600x600";
		String parameter = "";
		String temp = "";
		HibernateTemplate template = getHibernateTemplate();
		int j = 20;//template.loadAll(Station.class).size();
		//TODO 20 in getsize ändern
		for (int i = 1; i < j; i++) {
			
			if (this.findById(i) == null) {
			
			} else {
				temp = this.findById(i).getGoogle();
				parameter = parameter + "&markers=color:green|label:S|" + temp;
			}
		}
		googleLink = googleLink + parameter + "&sensor=false";
		return googleLink;
	}

	/**
	 * Deletes the specified {@link Station} object from the DB
	 * 
	 * @param station
	 *            the object to be deleted
	 * 
	 */
	// EXCEPTION VERSUCH
	// public void delete(Station station) throws StationDeleteException {
	// HibernateTemplate template = getHibernateTemplate();
	// Set<Auto> fahrtenVonAuto = autoDao.findByStation(station);
	// if (fahrtenVonAuto.size() == 0) {
	// template.delete(station);
	// } else {
	// throw new StationDeleteException();
	// }
	// }

	// ORIGINAL
	public void delete(Station station) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(station);
	}

	/**
	 * Returns all {@link Station} objects from the DB
	 * 
	 * @return a list of {@link Station} objects
	 */
	@SuppressWarnings("unchecked")
	public List<Station> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		@SuppressWarnings("rawtypes")
		List results = template.loadAll(Station.class);
		return results;
	}






}
